package com.wu.server.controller;

import com.wu.server.code.Code;
import com.wu.server.spider.JdSpider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author Wugs
 * @version 1.0
 * @date 2021/3/2 22:01
 */
@RestController
@CrossOrigin
public class SearchController {
    @Autowired
    JdSpider jdSpider;
    @Autowired
    Code code;
    @PostMapping("/search")
    @ResponseBody
    public HashMap<String,Object> getSearch(@RequestParam(value = "search",required = false) String search){
        HashMap<String ,Object> result = new HashMap<>();
        HashMap<String,Object> meta = new HashMap<>();
        meta.put("code",code.SUCCESS);
        meta.put("msg",code.getMsg(code.SUCCESS));
        spider spider = new spider();
        spider.setSearch(jdSpider,search);
        spider.start();
        result.put("meta",meta);

        return result;
    }
}
class spider extends Thread{
    private String search;
    JdSpider jdSpider;
    public void setSearch(JdSpider jdSpider,String search){
        this.jdSpider = jdSpider;
        this.search = search;
    }
    @Override
    public void run() {
        jdSpider.start(search);
    }
}
